package GUI;

import 试题和试卷.selectionQuestion;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import 试题和试卷.*;

public class selectionQuestionItem extends questionItem{

    JRadioButton aBut,bBut,cBut,dBut;
    JLabel questionInfo,question,answer;
    JScrollPane qScroller;
    public question currentquestion;
    int n;
    public  selectionQuestionItem(int n, question currentquestion){
        this.currentquestion=currentquestion;
        this.n=n;
        index=n-1;
        skipButton.setName(""+n);
        String qContent=currentquestion.getText();

        questionInfo = new JLabel("第"+n+"题  选择题");
        question = new JLabel("题目：");
        answer = new JLabel("解答：");

        Font bigFont = new Font("sanserif",Font.BOLD,24);

        aBut = new JRadioButton("A："+currentquestion.getOption_a());
        bBut = new JRadioButton("B："+currentquestion.getOption_b());
        cBut = new JRadioButton("C："+currentquestion.getOption_c());
        dBut = new JRadioButton("D："+currentquestion.getOption_d());

        JTextArea questionContent = new JTextArea(6,20);
        questionContent.setText(qContent);
        questionContent.setLineWrap(true);
        questionContent.setWrapStyleWord(true);
        questionContent.setFont(bigFont);
        questionContent.setEditable(false);

        qScroller = new JScrollPane(questionContent);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        questionInfo.setBounds(70,10,300,20);
        qScroller.setBounds(70,40,500,300);
        question.setBounds(20,40,50,30);
        aBut.setBounds(70,350,500,30);
        bBut.setBounds(70,390,500,30);
        cBut.setBounds(70,430,500,30);
        dBut.setBounds(70,470,500,30);

        aBut.addActionListener(new aListener());
        bBut.addActionListener(new bListener());
        cBut.addActionListener(new cListener());
        dBut.addActionListener(new dListener());

        answerSheet.buttons.add(new JButton(""+n));
        setButton(answerSheet.buttons.get(n));
        int selIndex=n;
        int selRow=selIndex/4;
        int selCol=selIndex%4;
        if (selIndex%4==0){
            selRow--;
            selCol=4;
        }
        answerSheet.buttons.get(n).setBounds(50+(selCol-1)*50,140+(selRow)*50,40,40);
        answerSheet.mainPanel.add(answerSheet.buttons.get(n));
        answerSheet.mainPanel.updateUI();
        /*for(int i= 1;i<answerSheet.selectionRow;i++){
            for(int j = 1;j<=4;j++){
                answerSheet.buttons.add(new JButton(""+n));
                setButton(answerSheet.buttons.get(n));
                answerSheet.buttons.get(n).setBounds(50+(j-1)*60,140+(i-1)*60,50,50);
                answerSheet.mainPanel.add(answerSheet.buttons.get(n));
                answerSheet.mainPanel.updateUI();
            }
        }
        for(int j = 1;j<=answerSheet.selectionQuestionNum%4;j++){
            answerSheet.buttons.add(new JButton(""+n));
            setButton(answerSheet.buttons.get(n));
            answerSheet.buttons.get(n).setBounds(50+(j-1)*60,140+(answerSheet.selectionRow-1)*60,50,50);
            answerSheet.mainPanel.add(answerSheet.buttons.get(n));
            answerSheet.mainPanel.updateUI();
        }*/
        answerSheet.buttons.get(n).addActionListener(new skipListener());


    }

    public class aListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(aBut.isSelected()){
                test.resultSet[1][index]="a";
                bBut.setSelected(false);
                cBut.setSelected(false);
                dBut.setSelected(false);
            }
        }
    }
    public class bListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(bBut.isSelected()){
                test.resultSet[1][index]="b";
                aBut.setSelected(false);
                cBut.setSelected(false);
                dBut.setSelected(false);
            }
        }
    }
    public class cListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(cBut.isSelected()){
                test.resultSet[1][index]="c";
                bBut.setSelected(false);
                aBut.setSelected(false);
                dBut.setSelected(false);
            }
        }
    }
    public class dListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if(dBut.isSelected()){
                test.resultSet[1][index]="d";
                bBut.setSelected(false);
                cBut.setSelected(false);
                aBut.setSelected(false);
            }
        }
    }

    public void display(){
        this.aBut.setText("A :"+currentquestion.getOption_a());
        this.bBut.setText("B :"+currentquestion.getOption_b());
        this.cBut.setText("C :"+currentquestion.getOption_c());
        this.dBut.setText("D :"+currentquestion.getOption_d());
        test.mainPanel.add(this.aBut);
        test.mainPanel.add(this.bBut);
        test.mainPanel.add(this.cBut);
        test.mainPanel.add(this.dBut);
        test.mainPanel.add(this.questionInfo);
        test.mainPanel.add(this.question);
        test.mainPanel.add(this.answer);
        test.mainPanel.add(this.qScroller);
    }

    public void remove(){
        test.mainPanel.remove(this.aBut);
        test.mainPanel.remove(this.bBut);
        test.mainPanel.remove(this.cBut);
        test.mainPanel.remove(this.dBut);
        test.mainPanel.remove(this.questionInfo);
        test.mainPanel.remove(this.question);
        test.mainPanel.remove(this.answer);
        test.mainPanel.remove(this.qScroller);
    }

    public void loadAnswer(){
        String key=test.resultSet[1][index];
        if(key!=null&&key.equals("a")){
            aBut.setSelected(true);
            bBut.setSelected(false);
            cBut.setSelected(false);
            dBut.setSelected(false);
        }else if(key!=null&&key.equals("b")){
            aBut.setSelected(false);
            bBut.setSelected(true);
            cBut.setSelected(false);
            dBut.setSelected(false);
        }else if(key!=null&&key.equals("c")){
            aBut.setSelected(false);
            bBut.setSelected(false);
            cBut.setSelected(true);
            dBut.setSelected(false);
        }else if(key!=null&&key.equals("d")){
            aBut.setSelected(false);
            bBut.setSelected(false);
            cBut.setSelected(false);
            dBut.setSelected(true);
        }
    }
    public void setButton(JButton button){
        button.setSize(40,40);
        button.setMargin(new Insets(0,0,0,0));
        button.setFont(new Font("宋体",Font.BOLD,14));
        button.setBackground(Color.lightGray);
    }
    public class skipListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){

            test.demo.dispose();
            test.demo=new test(n);
            test.mainPanel.updateUI();


        }
    }

}
